

function toggleOverlay(parentSpan,show){
	if(show){
		$("#" + parentSpan).show();
		$('#fillscreen').show();
		
		//you can add these to the document load event??
		if(v$("overlaymaster") && v$(parentSpan+"_scrollingarea")){
			setBounds(v$("overlaymaster"), v$(parentSpan).id,v$(parentSpan).id+"_scrollingarea");
		}

	} else {
		$("#" + parentSpan).hide();
		$('#fillscreen').hide();
		if(v$("overlaymaster") && v$(parentSpan+"_scrollingarea")){
			$("#overlaymaster").hide();
			resetBounds(v$(parentSpan).id,v$(parentSpan).id+"_scrollingarea");
		}
	}
}

var DimensionX1 = 0;
var DimensionY1 = 0;
var DimensionX2 = 0;
var DimensionY2 = 0;

function setBounds(masterelement, shellelement,contentelement){
		
	var paddingX = 0;
	var paddingY = 0;
	
	var defaultDimensionX1 = 0;
	var defaultDimensionY1 = 0;
	var defaultDimensionX2 = 0;
	var defaultDimensionY2 = 0;

	var scrollingDimensionX1;
	var scrollingDimensionY1;
	
	var scrollingDimensionX2;
	var scrollingDimensionY2;
	
	var windowDimensionX;
	var windowDimensionY;
	
	var objOuter = v$(shellelement);
	var objInner = v$(contentelement);
	
	//objOuter.style.display = "block";
	masterelement.style.display="block";
	
	if(objOuter){
		
		DimensionX1 = parseInt(objOuter.style.width);
		DimensionY1 = parseInt(objOuter.style.height);
		DimensionX2 = parseInt(objInner.style.width);
		DimensionY2 = parseInt(objInner.style.height);
			
		if(!objOuter.style.paddingLeft == "NaN"){
			paddingX = parseInt(objOuter.style.paddingLeft) + parseInt(objOuter.style.paddingRight);
		}else{
			//default to 20
			paddingX = 20;
		}
		
		defaultDimensionX1 = vPlacement(objOuter).width;
		defaultDimensionY1 = vPlacement(objOuter).height;
		
		defaultDimensionX2 = vPlacement(objInner).width;
		defaultDimensionY2 = vPlacement(objInner).height;
		
		//get the interior contents
		scrollingDimensionX1 = objOuter.scrollWidth;
		scrollingDimensionY1 = objOuter.scrollHeight;
		
		//get the interior contents
		scrollingDimensionX2 = objInner.scrollWidth;
		scrollingDimensionY2 = objInner.scrollHeight;
		
		masterelement.style.display="none";
		
		//get the browser dimensions so we can not exceed things
		if (window.innerWidth){
			//if browser supports window.innerWidth
			windowDimensionX = window.innerWidth;
			windowDimensionY = window.innerHeight;
		}
		else if (document.all) {
			//else if browser supports document.all (IE 4+)
			windowDimensionX = document.documentElement.clientWidth;
			windowDimensionY = document.documentElement.clientHeight;
		}
		
		var newX1 = 0;
		var newY1 = 0;
	
		var newX2 = 0;
		var newY2 = 0;
	
		//v$("test").innerHTML = ("windowDimensionX: " + windowDimensionX + ", scrollingDimensionX1: " + scrollingDimensionX1 + ", defaultDimensionX1: " + defaultDimensionX1);
		//v$("test").innerHTML += ("<br />windowDimensionX: " + windowDimensionX + ", scrollingDimensionX2: " + scrollingDimensionX2 + ", defaultDimensionX2: " + defaultDimensionX2);
		
		//v$("test").innerHTML += ("<br />windowDimensionY: " + windowDimensionY + ", scrollingDimensionY1: " + scrollingDimensionY1 + ", defaultDimensionY1: " + defaultDimensionY1);
		//v$("test").innerHTML += ("<br />windowDimensionY: " + windowDimensionY + ", scrollingDimensionY2: " + scrollingDimensionY2 + ", defaultDimensionY2: " + defaultDimensionY2);
		
		//secret lies within the outer container
		//dont ever set the outer to more than 20% of the total window width
		//take into account the scroll bar possibility
		if((scrollingDimensionX2 + paddingX + 16)>windowDimensionX){
			newX1 = defaultDimensionX1;
			newX2 = defaultDimensionX2 + 16;
		}else{
			newX1 = scrollingDimensionX2 + paddingX + 16;
			newX2 = newX1 - paddingX;
		}
		
		//v$("test").innerHTML += ("<br />Finished Calculations for new X values -> newX1: " + newX1 + ", newX2: " + newX2);
		
		objOuter.style.width = newX1 + 16 + "px";
		objInner.style.width = newX2 + 16 + "px";
			
		newY1 = scrollingDimensionY2 + 16 + (defaultDimensionY1 - defaultDimensionY2);
		if(newY1 > (windowDimensionY - (windowDimensionY*0.2)))
		{
			newY1 = (windowDimensionY - (windowDimensionY*0.2))
		}
		if(newY1 < defaultDimensionY1)
		{
			newY1 = defaultDimensionY1;
		}
		
		newY2 = newY1 - (defaultDimensionY1 - defaultDimensionY2);

		//v$("test").innerHTML += ("<br />Finished Calculations for new Y values -> newY1: " + newY1 + ", newY2: " + newY2);
		//padding for a65 modal shadows;
		objOuter.style.height = newY1 + "px";
		
		//should be -85 where sub portion only 1 element high
		//should be - 107 where sub portion 2 elements high
		//should be - 129 where sub portion 3 elements high
		
		objInner.style.height = (newY1 - 200) + "px";
		//objInner.style.height = newY2 + "px";
		
		//set the iframe of the overlay to the size of the outer box plus 22 to be safe
		v$("fillscreen").style.display = "none";
		v$("fillscreen_Iframe").style.width = objOuter.style.width;
		v$("fillscreen_Iframe").style.height = objOuter.style.height;
		v$("fillscreen").style.display = "block";
		
		masterelement.style.display="block";

		//this is the fix which will always size the div inside the table appropriately considering the padding.
		var _Offset = vPlacement(objInner.parentNode).height - 25;
		objInner.style.height = _Offset + "px";

		//position, but only for new modals
		if ($("#" + shellelement).hasClass("a65chromeModal")) {
			var thisElement = $("#" + shellelement);
			var xtop = 0;
			var xleft = 0;
			var parentW = $(window).width(), parentH = $(window).height();
			var childW = thisElement.width(), childH = thisElement.height();
			//alert((childH < parentH) ? ((parentH - childH) / 2) + $(window).scrollTop() - 50 : 10 + $(window).scrollTop());
			xtop = (childH < parentH) ? ((parentH - childH) / 2) + $(window).scrollTop() - 50 : 10 + $(window).scrollTop();
			xleft = (childW < parentW) ? ((parentW - childW) / 2) + $(window).scrollLeft() - 50 : 10 + $(window).scrollLeft();

			//hack for IE (and webkit now too)
			if ($.browser.msie || $.browser.webkit) {
				xtop = xtop - $(window).scrollTop();
				xleft = xleft - $(window).scrollLeft();
			}
			
			thisElement.css("top", xtop + "px").css("left", xleft + "px");
			
		}
	}
	
}

function resetBounds(shellelement,contentelement){
		
	var objOuter = v$(shellelement);
	var objInner = v$(contentelement);
	
	if(objOuter){
	
		objOuter.style.width = DimensionX1 + "px";
		objOuter.style.height = DimensionY1 + "px";

		if (objInner) {
		    objInner.style.width  = (isNaN(DimensionX2)) ? DimensionX1 + "px" : DimensionX2 + "px";
		    objInner.style.height = (isNaN(DimensionY2)) ? DimensionY1 + "px" : DimensionY2 + "px";
        }
		
		DimensionX1 = 0;
		DimensionY1 = 0;
		DimensionX2 = 0;
		DimensionY2 = 0;
		
		v$("fillscreen").style.display = "none";
		v$("fillscreen_Iframe").style.width = objOuter.style.width;
		v$("fillscreen_Iframe").style.height = objOuter.style.height;
			
	}
	
}